package com.basic.modules.ykf.controller;

import java.util.Arrays;

import cn.hutool.core.util.ObjectUtil;
import com.basic.common.exception.RRException;
import com.basic.common.utils.ParamsModel;
import com.basic.common.validator.ValidatorUtils;

import com.basic.modules.sys.entity.SysUserEntity;
import com.basic.modules.sys.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.basic.modules.ykf.entity.YkfUserAttendanceRecordEntity;
import com.basic.modules.ykf.service.YkfUserAttendanceRecordService;
import com.basic.common.utils.PageUtils;
import com.basic.common.utils.R;



/**
 * 用户请假管理
 *
 * @author maoheng
 * @email 15738859597@163.com
 * @date 2024-06-22 20:25:28
 */
@RestController
@RequestMapping("ykf/userAttendanceRecord")
public class YkfUserAttendanceRecordController {

    @Autowired
    private YkfUserAttendanceRecordService ykfUserAttendanceRecordService;

    @Autowired
    private SysUserService userService;

    /**
     * 列表-分页
     */
    @PostMapping("/page")
    public R list(@RequestBody ParamsModel<YkfUserAttendanceRecordEntity> params){
        PageUtils page = ykfUserAttendanceRecordService.queryPage(params);
        return R.ok(page);
    }


    /**
     * 信息
     */
    @PostMapping("/info")
    public R info(@RequestBody YkfUserAttendanceRecordEntity params){
        if (ObjectUtil.isEmpty(params) || ObjectUtil.isEmpty(params.getUserAttendanceRecordId())) {
            throw new RRException("参数缺失");
        }
        return R.ok(ykfUserAttendanceRecordService.getById(params.getUserAttendanceRecordId()));
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody YkfUserAttendanceRecordEntity ykfUserAttendanceRecord){
        ValidatorUtils.validateEntity(ykfUserAttendanceRecord);
        SysUserEntity user = userService.getById(ykfUserAttendanceRecord.getUserId());
        if (ObjectUtil.isNotEmpty(user)) {
            ykfUserAttendanceRecord.setUserName(user.getName());
        }
        ykfUserAttendanceRecordService.save(ykfUserAttendanceRecord);
        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public R update(@RequestBody YkfUserAttendanceRecordEntity ykfUserAttendanceRecord){
        ValidatorUtils.validateEntity(ykfUserAttendanceRecord);
        SysUserEntity user = userService.getById(ykfUserAttendanceRecord.getUserId());
        if (ObjectUtil.isNotEmpty(user)) {
            ykfUserAttendanceRecord.setUserName(user.getName());
        }
		ykfUserAttendanceRecordService.updateById(ykfUserAttendanceRecord);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public R delete(@RequestBody Long[] userLeaveRecordIds){
		ykfUserAttendanceRecordService.removeByIds(Arrays.asList(userLeaveRecordIds));
        return R.ok();
    }

}
